const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
var util = require('../../utils/util.js');
let sending=0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img1: "http://eplus.zokoo.com.cn/upload/wallet/plus.png",
    img2: "http://eplus.zokoo.com.cn/upload/wallet/plus.png",
    img3: "http://eplus.zokoo.com.cn/upload/wallet/plus.png",
    img4: "http://eplus.zokoo.com.cn/upload/wallet/plus.png",
    region: ['福建省', '厦门市', '思明区'],
    showarea: "请选择",
    state: 0,
    sexarr: ['是', '否'],
    sex: 0,
    sex2: 0,
    showsex: "是",
    showsex2: "否",
    objectMultiArray: [
      [
        {
          id: 0,
          name: '暂无分类'
        },
      ], [
        {
          id: 0,
          name: '暂无分类'
        }
      ]
    ],
    multiIndex: [0, 0],
    srctype: "选择类型",
    edittype:0,
    gou:'',
    auth:0,
    backcolor: 0,
    isauth:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    var time = util.formatTime(new Date());
    time = time.split(' ')[0];
    time = time.split('-');
    that.setData({
      year: time[0],
      month: time[1],
      day: time[2],
    })
    if (options.applycode == 'null' || options.applycode == null) {
      that.bigtype('');
    } else {
      var apinameIsMeb = "/BO/AP_ShopApply/Query";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          where: "ApplyCode = " + options.applycode 
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if (res.data.code == 0 && res.data.data.length > 0) {
            that.bigtype(res.data.data[0].RootCID);
            var srctype = res.data.data[0].LastCID ? res.data.data[0].LastCName : "请选择";
            var gps ='';
            if (res.data.data[0].Longitude){
              gps = "X:" + res.data.data[0].Latitude + ",Y:" + res.data.data[0].Longitude;
              if(gps.length >22){
                gps =gps.substring(0, 22) + "...";
              }
            }
            var showsex2 = res.data.data[0].IsVendor?"是":"否";
            var showsex = res.data.data[0].IsShop ? "是" : "否";
            that.setData({
              applyinfo: res.data.data[0],
              applyinfo2:res.data.data[0],
              idcardimg1: res.data.data[0].LicenceImg,
              idcardimg2: res.data.data[0].ShopImgUrl0,
              idcardimg3: res.data.data[0].ShopImgUrl1,
              idcardimg4: res.data.data[0].ShopImgUrl3,
              img1: res.data.data[0].LicenceImg,
              img2: res.data.data[0].ShopImgUrl0,
              img3: res.data.data[0].ShopImgUrl1,
              img4: res.data.data[0].ShopImgUrl3,
              showarea: res.data.data[0].ProName + "/" + res.data.data[0].CityName + "/" + res.data.data[0].AreaName,
              address: res.data.data[0].ProName + "/" + res.data.data[0].CityName + "/" + res.data.data[0].AreaName,
              state: res.data.data[0]._BOState ? res.data.data[0]._BOState:0,
              isauth: 1,
              id: res.data.data[0].ApplyCode,
              srctype: srctype,
              gps:gps,
              showsex2: showsex2,
              showsex:showsex,
              brandstr:res.data.data[0].Brands,
              regionname: res.data.data[0].RegionName,
              regionstr: res.data.data[0].RegionName ? res.data.data[0].RegionName:"",
              servicename:res.data.data[0].ServiceMebName,
              serviceid: res.data.data[0].ServiceMebID,
            })
          }
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // wx.getSetting({
    //   complete: function (res) {
    //     console.log(res);
    //     if (res.authSetting['scope.userLocation'] == true) {
    //       that.setData({
    //         mapauth: 1
    //       })
    //     } else {
    //       that.setData({
    //         mapauth: 0
    //       })
    //     }
    //   }
    // });
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      complete:function(res){
        console.log(res);
        if (res.errMsg == 'getLocation:fail auth deny'){
          wx.getSetting({
            complete: function (res) {
              console.log(res);
              if (res.authSetting['scope.userLocation'] == true) {
                that.setData({
                  mapauth: 1
                })
              } else {
                that.setData({
                  mapauth: 0
                })
              }
            }
          });
        }else{
          that.setData({
            mapauth: 1
          })
        }
      }
    })
    var brandstr = wx.getStorageSync('brandstr');
    if (brandstr.length > 16) {
      brandstr = brandstr.substring(0, 16);
      brandstr = brandstr + '...'
    }
    var auth = wx.getStorageSync('auth2');
    let regionstr ='';
    let regionname = wx.getStorageSync('regionname');
    if (regionname.length > 16) {
      regionstr = regionname.substring(0, 16);
      regionstr = regionstr + '...'
    }else{
      regionstr = regionname
    }
    
    if (auth == 1) {
      that.setData({
        gousign: 2,
        gou: '✔',
        backcolor: 1,
        auth:1,
        isauth:1
      })
    }
    that.setData({
      brandstr: brandstr,
      regionname: regionname,
      regionstr: regionstr,
    })
  },
  onUnload: function () {
   
  },
  uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "";

    var uuid = s.join("");
    return uuid;
  },
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      // multiArray: that.data.multiArray,
      multiIndex: that.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    // console.log(that.data.mularr[e.detail.value].id);
    var id = that.data.mularr[e.detail.value].id;
    switch (e.detail.column) {
      case 0:
        that.smalltype(id, that.data.mularr);
        data.multiIndex[1] = 0;
        break;
    }
    that.setData(data);
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(this.data.mularrs[e.detail.value[1]].id);
    this.setData({
      multiIndex: e.detail.value,
      srctype: this.data.mularrs[e.detail.value[1]].name,
      typeid: this.data.mularr[e.detail.value[0]].id + "-" + this.data.mularrs[e.detail.value[1]].id,
      typename: this.data.mularr[e.detail.value[0]].name + "-" + this.data.mularrs[e.detail.value[1]].name,
    })
  },
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bigtype: function (id) {
    var that = this;
    var datas = {};
    datas.ParentInvCID = 0;
    var datacall = function (res) {
      console.log(res);
      // return;
      if (res.data.data.length != 0) {
        var iii = {};
        var mularr = [];
        for (var i = 0; i < res.data.data.length; i++) {
          iii.id = res.data.data[i].InvCID;
          iii.name = res.data.data[i].InvCName;
          mularr[i] = iii;
          iii = {};
        }
        if (!id) {
          that.smalltype(mularr[0].id, mularr);
        } else {
          that.smalltype(id, mularr);
        }

      }
    }
    var res2 = post.getdata("/Action/ERP/GetInvClass", datas, datacall);
  },
  smalltype: function (id, arr) {
    var that = this;
    // 获取成员信息
    var datas = {};
    datas.ParentInvCID = id;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length != 0) {
        var iii = {};
        var mularrs = [];
        for (var i = 0; i < res.data.data.length; i++) {
          iii.id = res.data.data[i].InvCID;
          iii.name = res.data.data[i].InvCName;
          mularrs[i] = iii;
          iii = {};
        }

        that.setData({
          mularrs: mularrs,
          mularr: arr,
          objectMultiArray: [arr, mularrs]
        });
        // console.log(mularrs);
        return mularrs;
      }
    }
    var res2 = post.getdata("/Action/ERP/GetInvClass", datas, datacall);
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var info = e.detail.value;
    var that = this;
    if (info.licencecode == '') {
      wx.showModal({
        title: '',
        content: '营业执照号未填写',
      })
      return;
    }
    if (info.licencename == '') {
      wx.showModal({
        title: '',
        content: '营业执照号名称',
      })
      return;
    }
    if (info.name == '') {
      wx.showModal({
        title: '',
        content: '门店名称名称还未填写',
      })
      return;
    }
    if (info.tel == '') {
      wx.showModal({
        title: '',
        content: '店主手机号码还未填写',
      })
      return;
    }
    if (info.areasize == '') {
      wx.showModal({
        title: '',
        content: '营业面积尚未填写',
      })
      return;
    }
    if (info.img1 == '') {
      wx.showModal({
        title: '',
        content: '营业执照还未上传',
      })
      return;
    }
    if (info.img2 == '') {
      wx.showModal({
        title: '',
        content: '门店门头照片还未上传',
      })
      return;
    }
    if (info.img3 == '') {
      wx.showModal({
        title: '',
        content: '门店收银区照片还未上传',
      })
      return;
    }
    if (info.img4 == '') {
      wx.showModal({
        title: '',
        content: '门店经营区照片还未上传',
      })
      return;
    }
    if (info.address == '') {
      wx.showModal({
        title: '',
        content: '省市区还未填写',
      })
      return;
    }
    if(that.data.isauth==0){
      wx.showModal({
        title: '',
        content: '请先同意加盟协议',
      })
      return;
    }
    
    // 获取时间戳
 
    if (that.data.state == 100) {
      that.getaccount(that.data.applyinfo.DepID,info);
    }else{
      console.log(66);
      that.onsubmit({},info);
    }
    
  },
  chooseimg1(e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        var apinameIsMeb = "/File/Upload";
        var timestamp = new Date().getTime();
        timestamp = parseInt(timestamp / 1000);
        var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.server.url + apinameIsMeb, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file[]',
          method: 'POST',
          header: { 'content-type': 'multipart/form-data' },
          formData: {
            "sign": md5IsMeb,
            "appid": app.server.appid,
            "tmsp": timestamp,
            "objID": 666,
            "file": that.data.year + "/" + that.data.month + "/" + that.data.day + "/" + new Date().getTime() + Math.ceil(Math.random() * 1000) + ".jpg"
          },
          success: function (res) {
            wx.hideLoading();
            var data = res.data;
            data = JSON.parse(data);
            console.log(data.data)
            if (data.data != '') {
              that.setData({
                img1: "http://eplus.zokoo.com.cn/upload/" + data.data,
                idcardimg1: "http://eplus.zokoo.com.cn/upload/" + data.data
              })
            }
          }
        })
      }
    })
  },
  chooseimg2(e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        var apinameIsMeb = "/File/Upload";
        var timestamp = new Date().getTime();
        timestamp = parseInt(timestamp / 1000);
        var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.server.url + apinameIsMeb, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file[]',
          method: 'POST',
          header: { 'content-type': 'multipart/form-data' },
          formData: {
            "sign": md5IsMeb,
            "appid": app.server.appid,
            "tmsp": timestamp,
            "objID": 666,
            "file": that.data.year + "/" + that.data.month + "/" + that.data.day + "/" + new Date().getTime() + Math.ceil(Math.random() * 1000) + ".jpg"
          },
          success: function (res) {
            wx.hideLoading();
            var data = res.data;
            data = JSON.parse(data);
            console.log(data.data)
            if (data.data != '') {
              that.setData({
                img2: "http://eplus.zokoo.com.cn/upload/" + data.data,
                idcardimg2: "http://eplus.zokoo.com.cn/upload/" + data.data
              })
            }
          }
        })
      }
    })
  },
  chooseimg3(e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        var apinameIsMeb = "/File/Upload";
        var timestamp = new Date().getTime();
        timestamp = parseInt(timestamp / 1000);
        var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.server.url + apinameIsMeb, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file[]',
          method: 'POST',
          header: { 'content-type': 'multipart/form-data' },
          formData: {
            "sign": md5IsMeb,
            "appid": app.server.appid,
            "tmsp": timestamp,
            "objID": 666,
            "file": that.data.year + "/" + that.data.month + "/" + that.data.day + "/" + new Date().getTime() + Math.ceil(Math.random() * 1000) + ".jpg"
          },
          success: function (res) {
            wx.hideLoading();
            var data = res.data;
            data = JSON.parse(data);
            console.log(data.data)
            if (data.data != '') {
              that.setData({
                img3: "http://eplus.zokoo.com.cn/upload/" + data.data,
                idcardimg3: "http://eplus.zokoo.com.cn/upload/" + data.data
              })
            }
          }
        })
      }
    })
  },
  chooseimg4(e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        var apinameIsMeb = "/File/Upload";
        var timestamp = new Date().getTime();
        timestamp = parseInt(timestamp / 1000);
        var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.server.url + apinameIsMeb, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file[]',
          method: 'POST',
          header: { 'content-type': 'multipart/form-data' },
          formData: {
            "sign": md5IsMeb,
            "appid": app.server.appid,
            "tmsp": timestamp,
            "objID": 666,
            "file": that.data.year + "/" + that.data.month + "/" + that.data.day + "/" + new Date().getTime() + Math.ceil(Math.random() * 1000) + ".jpg"
          },
          success: function (res) {
            wx.hideLoading();
            var data = res.data;
            data = JSON.parse(data);
            console.log(data.data)
            if (data.data != '') {
              that.setData({
                img4: "http://eplus.zokoo.com.cn/upload/" + data.data,
                idcardimg4: "http://eplus.zokoo.com.cn/upload/" + data.data
              })
            }
          }
        })
      }
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      address: e.detail.value[0] + "/" + e.detail.value[1] + "/" + e.detail.value[2],
      showarea: e.detail.value[0] + "/" + e.detail.value[1] + "/" + e.detail.value[2]
    })
  },
  cancer: function () {
    var that = this;
    var apinameIsMeb = "/BO/AP_ShopApply/Approve";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        id: that.data.applyinfo.ApplyCode,
        state: 0
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          wx.switchTab({
            url: '/pages/mine/mine',
          })
        }
      }
    })
  },
  bindPickerChange2: function (e) {
    this.setData({
      sex: e.detail.value,
      showsex: this.data.sexarr[e.detail.value]
    })
  },
  bindPickerChange3: function (e) {
    this.setData({
      sex2: e.detail.value,
      showsex2: this.data.sexarr[e.detail.value]
    })
  },
  tobrand: function () {
    wx.navigateTo({
      url: '/pages/brandchoose/brandchoose',
    })
  },
  setting:function(e){
    var that = this;
    wx.showModal({
      title: '是否要打开设置页面',
      content: '需要获取您的位置信息，请到小程序的设置页面开启',
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          wx.openSetting({
            complete: function (res) {
              console.log(res);
              if (res.authSetting['scope.userLocation']==true){
                that.openmap();
              }
            }
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  openmap:function(){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    if (that.data.state == 0 || that.data.state == 5 || that.data.state == 100){
      that.choosemap();
    }else{
      that.showmap();
    }
  },
  choosemap: function () {
    var that =this;
    wx.getSetting({
      success(res) {
        console.log(res)
        // 2判断用户是否允许获取地理位置信息
        if (res.authSetting['scope.userLocation'] == false) {
          // 2.1调起用户授权设置，重新授权
          wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function (res) {
              wx.hideLoading();
              var latitude = res.latitude
              var longitude = res.longitude
              console.log(res);
              wx.chooseLocation({
                latitude: latitude,
                longitude: longitude,
                scale: 28,
                success(res) {
                  console.log(res)
                  var gps = "X:" + res.latitude + ",Y:" + res.longitude;
                  if (gps.length > 22) {
                    gps = gps.substring(0, 22) + "...";
                  }
                  that.setData({
                    gps: gps,
                    latitude:res.latitude,
                    longitude:res.longitude
                  })
                }
              })
            },
          })
        } else {
          wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function (res) {
              wx.hideLoading();
              var latitude = res.latitude
              var longitude = res.longitude
              console.log(res);
              wx.chooseLocation({
                latitude: latitude,
                longitude: longitude,
                scale: 28,
                success(res) {
                  var gps = "X:" + res.latitude + ",Y:" + res.longitude;
                  if (gps.length > 22) {
                    gps = gps.substring(0, 22) + "...";
                  }
                  that.setData({
                    gps: gps,
                    latitude: res.latitude,
                    longitude: res.longitude
                  })
                }
              })
            }
          })
        }
      },

      fail:function(res){
        console.log(res);
      }
    })
  },
  //查看地图
  showmap:function(){
    wx.getSetting({
      success(res) {
        console.log(res);
        // 2判断用户是否允许获取地理位置信息
        if (res.authSetting['scope.userLocation'] == false) {
          // 2.1调起用户授权设置，重新授权
          wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function (res) {
              wx.hideLoading();
              var latitude = res.latitude
              var longitude = res.longitude
              console.log(res);
              wx.openLocation({
                latitude: latitude,
                longitude: longitude,
                scale: 28,
                success(res) {

                }
              })
            }
          })
        } else {
          wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function (res) {
              wx.hideLoading();
              var latitude = res.latitude
              var longitude = res.longitude
              console.log(res);
              wx.openLocation({
                latitude: latitude,
                longitude: longitude,
                scale: 28,
                success(res) {

                }
              })
            }
          })
        }
      },
      fail:function(res){
        wx.hideLoading();
        console.log(res);
        return;
      }
    })
  },
  findservice: function () {
    var that = this;
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
        var datas = res.result.split('&');
        var mebarr = datas[0].split(':');
        if (mebarr[0] == 'mebid' && mebarr[1] != '') {
          console.log(111);
          var datas = {};
          datas.where = "MebID = " + mebarr[1];
          var datacall = function (res) {
            console.log(res);
            if (res.data.data.length != 0) {
              if (!res.data.data[0].IsService) {
                wx.showModal({
                  title: '',
                  content: '对方不是客户经理，无法添加',
                })
                return;
              } else {
                console.log("oktian");
                that.setData({
                  servicename: res.data.data[0].MebName,
                  serviceid: res.data.data[0].MebID
                })
               // that.addservice(res.data.data[0].MebID, res.data.data[0].MebName, res.data.data[0].Tel);
              }
            }
          }
          var res2 = post.getdata("/BO/BA_Member/Query", datas, datacall);

        } else {
          wx.showModal({
            title: '',
            content: '不是符合规定的二维码',
          })
        }
      }
    })
  },
  getaccount:function(accid,info){
    var that=this;
    var datas = {};
    datas.where = "AccID = " + accid;
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length > 0) {
        that.onsubmit(res.data.data[0],info)
      }
    }
    var res2 = post.getdata("/BO/TR_Account/Query", datas, datacall);
  },
  onsubmit:function(dd={},info){
    
    var that =this;
    var data = {};
    if(dd){
      data=dd;
      data.DepID=dd.AccID;  
    }
    var addressarr = info.address.split('/');
    var apinameIsMeb = "/BO/AP_ShopApply/Add";
    if (that.data.id && that.data.state != 100) {
      data.ApplyCode = that.data.id;
      apinameIsMeb = "/BO/AP_ShopApply/Modify";
    }
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);

    data.DepName = info.name;
    data.PsnName = info.shopname;
    data.Tel = info.tel;
    data.AreaSize = info.areasize;
    data.MebID = wx.getStorageSync('userinfo').MebID;
    data.LicenceImg = info.img1;
    data.LicenceName = info.licencename;
    data.ShopImgUrl0 = info.img2;
    data.ShopImgUrl1 = info.img3;
    data.ShopImgUrl3 = info.img4;
    data.ProName = addressarr[0] ? addressarr[0]:"";
    data.CityName = addressarr[1] ? addressarr[1]:"";
    data.AreaName = addressarr[2] ? addressarr[2]:"";
    data.AddressDetail = info.detailaddress;
    data.LicenceCode = info.licencecode;
    data.PUBackRate = info.backrate;
    data.ServiceArea = info.servicearea;
    data.Brands = wx.getStorageSync('brandstr') + '';
    if (that.data.regionname){
      data.RegionName = that.data.regionname;
    }
    if (info.isgongying == '是') {
      data.IsVendor = true;
      if (!that.data.typeid) {
        wx.showModal({
          title: '',
          content: '还未选择供应类目',
        })
        
        return;
      }
      if (!data.Brands) {
        wx.showModal({
          title: '',
          content: '还未选择供应品牌',
        })
        return;
      } else {
        var brandss = data.Brands.split(';');
        var brands = '';
        for (var i = 0; i < brandss; i++) {
          if (brandss[i]) {
            brands += brandss[i] + ";";
          }
        }
      }
      if (!data.ServiceArea) {
        wx.showModal({
          title: '',
          content: '还没填写配送区域范围',
        })
        return;
      }
      if (!data.PUBackRate || data.PUBackRate < 1) {
        wx.showModal({
          title: '',
          content: '采购返点未设置或设置点数小于1%',
        })
        return;
      }
    } else {
      data.IsVendor = false;
    }
    if (that.data.typeid) {
      data.RootCID = that.data.typeid.split('-')[0];
      data.LastCID = that.data.typeid.split('-')[1];
      data.RootCName = that.data.typename.split('-')[0];
      data.LastCName = that.data.typename.split('-')[1];
    }
    if (that.data.latitude) {
      data.Latitude = that.data.latitude;
      data.Longitude = that.data.longitude;
    }
    data.IsShop = true;
    data.ServiceMebID = that.data.serviceid ? that.data.serviceid : "";
    data._BOState = 10;
    console.log(data);
    data = JSON.stringify(data);

    wx.showLoading({
      title: '正在提交,请稍候...',
    })
    sending++;
    if (sending != 1) {
      return;
    }
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        data: data
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        wx.hideLoading();
        sending=0;
        that.setData({
          subsign: 1
        })
        console.log(res);
        if (res.data.code == 0) {
          wx.navigateBack({
            delta: 1
          })
          // wx.switchTab({
          //   url: '/pages/mine/mine',
          // })
        } else {
          wx.showModal({
            title: '提交出错',
            content: res.data.msg,
          })
          sending=0;
          console.log(res);
          return;
        }
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showModal({
          title: '',
          content: "请求失败",
        })
        console.log(res);
        return;
      }
    })
  },
  toedit:function(){
    if(this.data.edittype==0){
      return;
    }
    var applyinfo2 = this.data.applyinfo2;
    var gps = '';
    if (applyinfo2.Longitude) {
      gps = "X:" + applyinfo2.Latitude + ",Y:" + applyinfo2.Longitude;
      if (gps.length > 22) {
        gps = gps.substring(0, 22) + "...";
      }
    }
    this.setData({
      edittype:0,
      applyinfo:applyinfo2,
      idcardimg1: applyinfo2.LicenceImg,
      idcardimg2: applyinfo2.ShopImgUrl0,
      idcardimg3: applyinfo2.ShopImgUrl1,
      idcardimg4: applyinfo2.ShopImgUrl3,
      img1: applyinfo2.LicenceImg,
      img2: applyinfo2.ShopImgUrl0,
      img3: applyinfo2.ShopImgUrl1,
      img4: applyinfo2.ShopImgUrl3,
      showarea: applyinfo2.ProName + "/" + applyinfo2.CityName + "/" + applyinfo2.AreaName,
      address: applyinfo2.ProName + "/" + applyinfo2.CityName + "/" + applyinfo2.AreaName,
      state: applyinfo2._BOState ? applyinfo2._BOState : 0,
      id: applyinfo2.ApplyCode,
      gps: gps,
      brandstr: applyinfo2.Brands,
      servicename: applyinfo2.ServiceMebName,
      serviceid: applyinfo2.ServiceMebID,
    })
  },
  tocreate: function () {
    if (this.data.edittype == 1) {
      return;
    }
    this.setData({
      edittype: 1,
      applyinfo:[],
      img1: "http://eplus.zokoo.com.cn/upload/wallet/plus.png",
      img2: "http://eplus.zokoo.com.cn/upload/wallet/plus.png",
      img3: "http://eplus.zokoo.com.cn/upload/wallet/plus.png",
      img4: "http://eplus.zokoo.com.cn/upload/wallet/plus.png",
      idcardimg1: '',
      idcardimg2: '',
      idcardimg3: '',
      idcardimg4: '',
      id:null,
      state:0,
      gps:''
    })
  },
  beagree: function () {
    // console.log(this.data.gousign);
    if (this.data.gousign == 1) {
      this.setData({
        gousign: 2,
        gou: '✔',
        backcolor: 1,
        isauth:1
      })
    } else {
      this.setData({
        gousign: 1,
        gou: '',
        backcolor: 0,
        isauth: 0
      })
    }
  },
  toprotocol: function () {
    wx.navigateTo({
      url: '/pages/agreement/agreement',
    })
  },
  tostreet:function(){
    if(this.data.showarea=='请选择'){
      wx.showModal({
        title: '提示',
        content: '请先选择省市区！',
      })
      return;
    }
    let areas = this.data.showarea.split('/');
    let province = areas[0] ? areas[0] : "no";
    let city = areas[1] ? areas[1] : "no";
    let area = areas[2] ? areas[2] : "no";
    if(province=='no'){
      wx.showModal({
        title: '提示',
        content: '请先选择省市区！',
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/street/street?province='+province+"&city="+city+"&area="+area,
    })
  }

})